import { Ast } from "../ast";
import { CoreVisitor } from "../core";

export interface RedPacketInfo {
    type?: string;
    count?: number;
    unit?: string;
    password?: string;
    desc?: string;
    sub_title?: string;
    total?: string;
    title?: string;
    join?: () => void;
    close?: () => void;
    follow?: () => void;
}
export class RedPacketInfoVisitor extends CoreVisitor<RedPacketInfo> {
    visitRelativeLayout(node: Ast, ctx: RedPacketInfo) {
        if (node.id.endsWith(`action_view`) && node.clickable) {
            ctx.join = () => node.click()
            return;
        }
        return node.children.map(c => c.visit(this, ctx))
    }
    visitImageView(node: Ast, ctx: RedPacketInfo) {
        if (node.id && node.id.endsWith(`close_view`)) {
            ctx.close = () => node.click()
            return;
        }
        if (node.id && node.id.endsWith(`red_packet_reward_source_follow_image`)) {
            ctx.follow = () => node.click()
            return;
        }
        if (node.id && node.id.endsWith(`open_view`)) {
            ctx.join = () => node.click()
            return;
        }
    }
    visitTextView(node: Ast, ctx: any) {
        if (node.sid === `live_condition_leee_button_text` && node.text === `去评论`) {
            ctx.join = () => node.parent.click()
            return;
        }
        if (node.id && node.id.endsWith(`common_red_packet_pop_prepare_reward_desc_view`) || node.id.endsWith(`red_packet_pop_prepare_source_desc_view`) || node.id.endsWith(`title_text_view`)) {
            const res = node.text.match(/主播的(.*?)红包/)
            if (res && res.length > 0) {
                const [, type] = res;
                ctx.type = type;
            }
            else if (/.*?穿云箭.*?/.test(node.text)) {
                ctx.type = `穿云箭`
            }
            else if (/.*?的快币红包.*?/.test(node.text)) {
                ctx.type = `快币`
            }
            else if (/.*?礼物红包.*?/.test(node.text)) {
                ctx.type = `礼物`
            }
            else {
                ctx.type = `其他`
            }
            ctx.title = node.text;
        }
        if (node.id && node.id.endsWith(`red_packet_cover_reward_sub_pack_count`) || node.id.endsWith(`live_sec_kill_red_packet_reward_gift_item_tag`)) {
            ctx.total = parseInt(node.text.replace('x', ''))
        }
        if (node.id && node.id.endsWith(`red_packet_cover_reward_usual_count`) || node.id.endsWith(`live_sec_kill_red_packet_pop_prepare_reward_kcoin_view`)) {
            ctx.count = parseInt(node.text);
        }
        if (node.id && node.id.endsWith(`red_packet_cover_reward_usual_unit`) || node.id.endsWith(`live_sec_kill_prepare_reward_desc_text_view`)) {
            ctx.unit = node.text
        }
        if (node.id && node.id.endsWith(`prepare_reward_password_text_view`)) {
            ctx.password = node.text
            return;
        }
        if (node.id && node.id.endsWith(`second_line_text_view`) || node.id.endsWith(`tips_text_view`)) {
            ctx.desc = node.text;
            return;
        }
        if (node.id && node.id.endsWith(`first_line_text_view`)) {
            ctx.sub_title = node.text
            return;
        }
    }
}
